<?php
/**
 * Created by PhpStorm.
 * User: 大帅
 * Date: 15-4-22
 * Time: 上午10:36
 */
use \PFT\Db;
class Version{
    private static $med=null;
    private static $status=null; //1.成功 2.失败 3.超出权限
    private function exp($med,$status){
        switch($status){
            case 0:
                $script=<<<html
<script>alert('信息{$med}失败!标题、内容不能为空！');history.go(-1);</script>
html;
                throw new \Exception($script);
                break;
            case 1:
                $script=<<<html
<script>alert('信息{$med}成功');parent.location.href="http://www.12301.cc/module/pwk/home.html";</script>
html;
                throw new \Exception($script);
                break;
            case 2:
                $script=<<<html
<script>alert('信息{$med}失败');history.go(-1);</script>
html;
                throw new \Exception($script);
                break;
            case 3:
                $script=<<<html
<script>alert('超出权限');parent.location.href="http://www.12301.cc/module/pwk/home.html";</script>
html;
                throw new \Exception($script);
                break;
            default:
                break;
        }

    }
    public function ver_add($title,$content,$dtype,$time){
        $str=<<<SQL
INSERT INTO pft_update_log (uplog_title,uplog_content,access_date) VALUES ('{$title}','{$content}','{$time}');
SQL;
        try{
            $a=version::$med="提交";
            switch($dtype){
                case 9:
                    $row=\PFT\Db::Connect()->exec($str);
                    if(!empty($title)&&!empty($content)){
                        if($row){
                            $this->exp($a,1);
                        }else{
                            $this->exp($a,2);
                        }
                    }else{
                        $this->exp($a,0);
                    }
                    break;
                default:
                    $this->exp($a,3);
                    break;
            }
        }
        catch(\Exception $e) {
            echo $e->getMessage();
        }
    }
    public function ver_del($id,$dtype){
        $str=<<<SQL
delete from pft_update_log where id='{$id}' limit 1;
SQL;
        try{
            $a=version::$med="删除";
            switch($dtype){
                case 9:
                    $row=\PFT\Db::Connect()->exec($str);
                    if($row){
                        $this->exp($a,1);
                    }else{
                        $this->exp($a,2);
                    }
                    break;
                default:
                    $this->exp($a,3);
                    break;
            }
        }
        catch(\Exception $e) {
            echo $e->getMessage();
        }
    }
    public function ver_upd($id,$title,$content,$dtype,$time){
        $str=<<<SQL
update pft_update_log set uplog_title='{$title}',uplog_content='{$content}',access_date='{$time}' where id='{$id}'
limit 1;
SQL;
        try{
            $a=version::$med="更新";
            switch($dtype){
                case 9:
                    $row=\PFT\Db::Connect()->exec($str);
                    if($row){
                        $this->exp($a,1);
                    }else{
                        $this->exp($a,2);
                    }
                    break;
                default:
                    $this->exp($a,3);
                    break;
            }
        }
        catch(\Exception $e) {
            echo $e->getMessage();
        }
    }
    public function ver_sel($limit,$id=NULL){

        if(is_null($id)){
        $str ="select id,uplog_title,uplog_content from pft_update_log order by id desc limit {$limit}";
        }else{
        $str =<<<html
select uplog_title,uplog_content,access_date from pft_update_log where id={$id};
html;
        }
        $stmt =\PFT\Db::Connect()->prepare($str);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
    public function ver_more(){
        $str ="select id,uplog_title,uplog_content from pft_update_log order by id desc";
        $stmt =\PFT\Db::Connect()->prepare($str);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}